#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const ll N = 1e6 + 10, INF = 0x3f3f3f3f;

ll n, d;
string s;

void solve()
{
    cin >> n >> d;
    cin >> s;
    sort(s.begin(), s.end());
    // if (s.find('0') != string::npos) { cout << 0; return; }

    queue<pair<string, ll>> q;
    q.push({ "",0 });
    while (!q.empty()) {
        auto [now, st] = q.front();q.pop();
        if (now.length() != 0 && stoll(now) % d == 0) { cout << now; return; }
        for (ll i = 0;i < s.length();i++) {
            if (st & (1 << i)) continue;
            q.push({ now + s[i], st | (1 << i) });
        }
    }
    cout << -1;
}

signed main()
{
    //ios::sync_with_stdio(0);
    //cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    ll T = 1;
    //cin>>T;
    for (ll i = 1;i <= T;i++) {
        solve();
    }
}